package com.ruoyi.shangpinfenlei.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.shangpinfenlei.domain.WxCategory;
import com.ruoyi.shangpinfenlei.service.IWxCategoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author ruoyi
 * @date 2025-09-11
 */
@RestController
@RequestMapping("/shangpinfenlei")
public class WxCategoryController extends BaseController
{
    @Autowired
    private IWxCategoryService wxCategoryService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("shangpinfenlei:shangpinfenlei:list")
    @GetMapping("/list")
    public TableDataInfo list(WxCategory wxCategory)
    {
        startPage();
        List<WxCategory> list = wxCategoryService.selectWxCategoryList(wxCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("shangpinfenlei:shangpinfenlei:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxCategory wxCategory)
    {
        List<WxCategory> list = wxCategoryService.selectWxCategoryList(wxCategory);
        ExcelUtil<WxCategory> util = new ExcelUtil<WxCategory>(WxCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("shangpinfenlei:shangpinfenlei:query")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(wxCategoryService.selectWxCategoryByCategoryId(categoryId));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("shangpinfenlei:shangpinfenlei:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxCategory wxCategory)
    {
        return toAjax(wxCategoryService.insertWxCategory(wxCategory));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("shangpinfenlei:shangpinfenlei:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxCategory wxCategory)
    {
        return toAjax(wxCategoryService.updateWxCategory(wxCategory));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("shangpinfenlei:shangpinfenlei:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(wxCategoryService.deleteWxCategoryByCategoryIds(categoryIds));
    }

    /**
     * 获取父类别列表
     */
    @GetMapping("/parents")
    public AjaxResult getParentCategories()
    {
        WxCategory wxCategory = new WxCategory();
        wxCategory.setParentCategoryId(null); // 父类别ID为null表示顶级分类
        List<WxCategory> list = wxCategoryService.selectWxCategoryList(wxCategory);
        return success(list);
    }

    /**
     * 根据父类别ID获取子类别列表
     */
    @GetMapping("/children/{parentCategoryId}")
    public AjaxResult getChildCategories(@PathVariable("parentCategoryId") Long parentCategoryId)
    {
        WxCategory wxCategory = new WxCategory();
        wxCategory.setParentCategoryId(parentCategoryId);
        List<WxCategory> list = wxCategoryService.selectWxCategoryList(wxCategory);
        return success(list);
    }
}
